package server.core;

import org.apache.catalina.*;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

public class SimpleWrapperValve implements Valve, Contained {
    private Container container;

    //实现Contained接口----------------------------------------------------------------------
    @Override
    public Container getContainer() {
        return container;
    }

    @Override
    public void setContainer(Container container) {
        this.container = container;
    }

    //实现Valve接口----------------------------------------------------------------------
    @Override
    public String getInfo() {
        return null;
    }

    @Override
    public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException {
        Wrapper wrapper = (Wrapper)container;
        Context context =  (Context) wrapper.getParent();
        request.setContext(context);
        Servlet servlet = wrapper.allocate();
        servlet.service(request.getRequest(), response.getResponse());
    }
}
